﻿using System;
using System.Web;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.Helpers;
using Senparc.Weixin.MP.Entities;

namespace Weixin.Sample
{
    public class AspHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            string responseitem = string.Empty;

            if (request.HttpMethod == "POST")
            {
                var requestMessage = RequestMessageFactory.GetRequestEntity(request.InputStream);

                switch (requestMessage.MsgType)
                {
                    case RequestMsgType.Text:
                        responseitem = Execute_Text(requestMessage as RequestMessageText);
                        break;
                    case RequestMsgType.Location:

                        break;
                    case RequestMsgType.Link:
                        break;
                    case RequestMsgType.Image:
                        break;
                    case RequestMsgType.Video:
                        break;
                    case RequestMsgType.Voice:
                        break;
                    case RequestMsgType.Event:
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion

        private string Execute_Text(RequestMessageText request)
        {
            //request.CreateResponseMessage<ResponseMessageText>();

            var response = ResponseMessageBase.CreateFromRequestMessage<ResponseMessageText>(request);
            response.Content = "您输入的文本信息是: " + request.Content;
            return response.ConvertEntityToXmlString();
        }
    }
}
